package com.imti.mall.sale.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.imti.mall.common.Api.CommonPage;
import com.imti.mall.common.Api.CommonResult;
import com.imti.mall.model.sms.pojo.SmsCoupon;
import com.imti.mall.model.sms.pojo.SmsCouponHistory;
import com.imti.mall.model.sms.pojo.SmsCouponProductCategoryRelation;
import com.imti.mall.model.sms.pojo.SmsCouponProductRelation;
import com.imti.mall.model.sms.vo.CouponHistoryQuery;
import com.imti.mall.model.sms.vo.CouponQuery;
import com.imti.mall.sale.service.SmsCouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

@RestController
@Api("营销管理")
@RequestMapping("/api/v1/sale")
public class SmsCouponController {

  @Autowired
  private SmsCouponService smsCouponService;

  @ApiOperation(value = "优惠券列表")
  @GetMapping("/coupon/list")
  public CommonResult getSaleCouponList(CouponQuery couponQuery, @RequestParam("pageNum") Integer pageNum,
                                        @RequestParam("pageSize") Integer pageSize) {

    List<SmsCoupon> smsCouponList = smsCouponService.getList(couponQuery,pageNum, pageSize);
    return CommonResult.success(CommonPage.restPage(smsCouponList));
  }


  @ApiOperation("查看优惠券id的详情信息")
  @GetMapping("/coupon/{id}")
  public CommonResult getCouponById(@PathVariable Long id) {
    SmsCoupon smsCoupon = smsCouponService.getCouponById(id);

    if(smsCoupon.getUseType()==1){
      //当前优惠券为指定分类的优惠券
      List<SmsCouponProductCategoryRelation> smsCouponProductCategoryRelationList = smsCouponService.getProductCateRelationByCouponId(id);
      smsCoupon.setSmsCouponProductCategoryRelationList(smsCouponProductCategoryRelationList);
    }
    if(smsCoupon.getUseType()==2){
      //当前优惠券为指定商品的优惠券
      List<SmsCouponProductRelation> smsCouponProductRelationList = smsCouponService.getCouponProductById(id);
      smsCoupon.setSmsCouponProductRelationList(smsCouponProductRelationList);
    }else{

    }
    return CommonResult.success(smsCoupon);
  }

  @ApiOperation("编辑优惠券")
  @PostMapping("/coupon/update")
  public CommonResult updateCouponById(@RequestBody SmsCoupon smsCoupon) throws ParseException {
    int result = smsCouponService.updateCouponById(smsCoupon);
    return CommonResult.success(result);
  }

  @ApiOperation("新增优惠券")
  @PostMapping("/coupon/create")
  public CommonResult addCouponMsg(@RequestBody SmsCoupon smsCoupon){
    smsCoupon.setPublishCount(smsCoupon.getCount());
    smsCoupon.setUseCount(0);
    int result = smsCouponService.addCouponMsg(smsCoupon);
    return CommonResult.success(result);
  }

  @ApiOperation("删除优惠券")
  @PostMapping("/coupon/delete/{id}")
  public CommonResult deleteCoupon(@PathVariable Long id){
    int result = smsCouponService.deleteCoupon(id);
    return CommonResult.success(result);
  }

  @ApiOperation("优惠券使用情况")
  @GetMapping("/couponHistory/list")
  public CommonResult getHistoryList(CouponHistoryQuery couponHistoryQuery, @RequestParam("pageNum") Integer pageNum,
                                     @RequestParam("pageSize") Integer pageSize){
    CommonPage commonPage = new CommonPage();
    List<SmsCouponHistory> smsCouponHistoryList = smsCouponService.getHistoryList(couponHistoryQuery,pageNum, pageSize);
    long total = smsCouponService.getHistoryCount(couponHistoryQuery);
    commonPage.setTotal(total);
    int totalPage = (int) Math.ceil(total / pageSize);
    commonPage.setTotalPage(totalPage);
    commonPage.setPageNum(pageNum);
    commonPage.setPageSize(pageSize);
    commonPage.setList(smsCouponHistoryList);
    return CommonResult.success(commonPage);
  }
}
